#debug or release
#TYPE = release 
TYPE = debug

#source file directories
SRC_DIR = ./src 

#include file directories
INC_DIR = ./inc

#output lib dir
LIB_DIR = ./lib
DLL_DIR = ./dll

#depended lib dir
DEPLIB_DIR = $(LIB_DIR)/3rd

# 3rd libs
DEPLIBS =

#sys libs
SYSLIBS = -lm

#bin dir
BIN_DIR = ./bin

#specify the name of the output static lib and shared lib
BASE = hello
LIB = lib${BASE}.a
DLL = lib${BASE}.so

CC = gcc
CXX = g++
CFLAGS = -Wall

#adjust $(CLFAGS) according to $(TYPE)
ifeq ($(TYPE), debug)
	CFLAGS += -g3 -pg -DDEBUG
else
ifeq ($(TYPE), release)
	CFLAGS += -O3
else
	CFLAGS += -g -O
endif
endif

CFLAGS += $(addprefix -I, $(INC_DIR))
CFLAGES += $(addprefix -L, $(DEPLIB_DIR))
CXXFLAGS = $(CFLAGS)

VPATH = $(addprefix :, $(SRC_DIR))

#get the source file names from the source dirs
SRCS := $(wildcard $(addsuffix /*.c, $(SRC_DIR))) \
	$(wildcard $(addsuffix /*.cpp, $(SRC_DIR))) \
	$(wildcard $(addsuffix /*.CPP, $(SRC_DIR)))

#get the obj file names from the soruce file names
OBJS := $(notdir $(SRCS:.c=.o))
OBJS := $(OBJS:.cpp=.o)
OBJS := $(OBJS:.CPP=.o)

DEPS := $(OBJS:.o=.d)

.PHONY:all exe lib dll depend clean

all: exe lib dll

exe: $(EXE);

lib: ;

dll: $(DLL)

clean:
	-rm *.o *.d dll/* lib/* bin/*

$(DLL): $(OBJS)
	$(CC) $(CFLAGS) -fPIC -shared $(DEPLIBS) $(SYSLIBS) $(OBJS) -o $(DLL_DIR)/$@

$(EXE): $(OBJS)
	$(CC) $(CFLAGS) $(DEPLIBS) $(SYSLIBS) $(OBJS) -o $(BIN_DIR)/$@

%.d: %.c
	@$(CC) -MM $(CFLAGS) $< > $@
%.d: %.cpp
	@$(CXX) -MM $(CXXFLAGS) $< > $@
%.d: %.CPP
	@$(CXX) -MM $(CXXFLAGS) $< > $@

depend: $(DEPS)
	@echo depends are all up-to-date.
-include $(DEPS)
